package com.greenline.search.test.test;

import com.greenline.search.report.entity.SearchWord;
import com.greenline.search.report.model.Configuration;
import com.greenline.search.report.util.FileUtils;
import com.greenline.search.test.httprequest.MyHttpRequest;
import com.greenline.search.test.httprequest.ResponseInfo;
import com.greenline.search.test.httprequest.impl.MyHttpClientRequest;
import com.greenline.search.test.utils.JSONStreamParserForAnalyze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 14-2-14
 * Time: 下午2:31
 * To change this template use File | Settings | File Templates.
 */
public class Top500NoResultQueryWord {
    private static Logger logger = LoggerFactory.getLogger(Top500NoResultQueryWord.class);
    private String logFolderName = "201401";
    private String doctorSearch = "http://10.10.100.153:9999/doctor_search?fl=doctor_uuid&secondsort=0&rows=0&q=";

    public List<SearchWord> getAllSearchWord(){
        List<SearchWord> searchWordList = new LinkedList<SearchWord>();
        String logFolderPath = Configuration.ROOT_PATH+logFolderName;
        File logFolder = new File(logFolderPath);
        for(File file : logFolder.listFiles()){
            logger.info("Process the file {}", file.getName());
            for(String content : FileUtils.readFileAsString(file.getPath(),"UTF-8")){
                SearchWord searchWord;
                String[] contentSplit = content.split("\"\t\"");
                try {
                    if(contentSplit[0].length() > 1){
                        String word = contentSplit[0].substring(1,contentSplit[0].length());
                        searchWord = JSONStreamParserForAnalyze.getWordType(word);
                        searchWord.setText(word);
                        searchWord.setFrequency(Integer.parseInt(contentSplit[1]));
                        if(searchWordList.contains(searchWord)){
                            int wordIndex = searchWordList.indexOf(searchWord);
                            SearchWord originalSearchWord = searchWordList.get(wordIndex);
                            searchWord.setFrequency(searchWord.getFrequency()+originalSearchWord.getFrequency());
                            searchWordList.set(wordIndex,searchWord);
                        } else {
                            searchWordList.add(searchWord);
                        }
                    }else {
                        continue;
                    }
                }catch (StringIndexOutOfBoundsException e){
                    e.printStackTrace();
                }
            }
        }
        return searchWordList;
    }

    //获取搜索结果为0的前needCount个searchword
    public List<SearchWord> getNoResultSearchWord(List<SearchWord> originalSearchWordList,
                                                  Integer needWordCount) throws UnsupportedEncodingException {
        List<SearchWord> noResultSearchWordList = new ArrayList<SearchWord>();
        MyHttpRequest httpRequest = new MyHttpClientRequest();
        for(int i = 0; i < originalSearchWordList.size(); i++){
            if(i % 501 == 0){
                logger.info("Deal with {} word already.", i+1);
            }
            ResponseInfo doctorResponseInfo = httpRequest.requestAndGetResponseInfo(
                    doctorSearch + URLEncoder.encode(originalSearchWordList.get(i).getText(), "UTF-8"));
            if(noResultSearchWordList.size() < needWordCount && doctorResponseInfo.getReturnCount() == 0){
                noResultSearchWordList.add(originalSearchWordList.get(i));
            }
            if(noResultSearchWordList.size() >= needWordCount){
                break;
            }
        }
        return noResultSearchWordList;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        Top500NoResultQueryWord object = new Top500NoResultQueryWord();
        List<SearchWord> searchWordList = object.getAllSearchWord();
        Collections.sort(searchWordList);
        List<SearchWord> noResultWordList = object.getNoResultSearchWord(searchWordList,500);
        StringBuilder sb = new StringBuilder();
        String tagStr = "\t";
        for(SearchWord searchWord : noResultWordList){
            sb.append(searchWord.getText()).append(tagStr);
            sb.append(searchWord.getFrequency()).append(tagStr);
            sb.append(Arrays.toString(searchWord.getTokenArray())).append(tagStr);
            sb.append(Arrays.toString(searchWord.getTypeArray())).append("\n");
        }
        logger.info(sb.toString());

    }

}
